package com.yopuo.common.utils.bean;

import com.yopuo.common.utils.StringUtils;
import java.lang.reflect.Field;
import java.util.*;

/**
 * 请求参数封装map
 * 
 * @author zzh
 */
public abstract class Params
{
    private static final String SYSDATE_KEY = "sysDate";
    private static final String DELETED = "deleted";
    private static final String LIKE_PREFIX = "%";
    private final Map<String,Object> data = new HashMap<>();

    /**
     * 构造函数
     * @return
     */
    public static Params create(){
        return new ParamsMap();
    }

    /**
     * 封装参数，值
     */
    public Params addParams(String paramName,Object paramValue) {
        if (this.canAdd(paramValue)){
            this.data.put(paramName,paramValue);
        }
        return this;
    }

    /**
     * 封装实体类数据.
     */
    public Params addEntity(Object obj) {
        try {
            if (this.canAdd(obj)){
                //获取父类的属性和属性值
                Field[] fieldsuper = obj.getClass().getFields();
                if (fieldsuper.length > 0) {
                    for (Field field : fieldsuper) {
                        field.setAccessible(true);
                        if (field.get(obj) != null) {
                            this.data.put(field.getName(), field.get(obj));
                        }
                    }
                }

                //获取类的属性和属性值
                Field[] fields = obj.getClass().getDeclaredFields();
                for (Field field : fields) {
                    field.setAccessible(true);
                    if (field.get(obj) != null) {
                        this.data.put(field.getName(), field.get(obj));
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return this;
    }

    public Params addSysDate(){
        this.data.put(SYSDATE_KEY,new Date());
        return this;
    }

    public Params add(Map<String,Object> params){
        if (null != params){
            for (Map.Entry<String,Object> entry : params.entrySet()){
                if (this.canAdd(entry.getValue())){
                    this.data.put(entry.getKey(),entry.getValue());
                }
            }
        }
        return this;
    }

    public Params addDeleted(String deleted){
        return this.addParams(DELETED,deleted);
    }

    /**
     * 左模糊查询
     * @param key
     * @param value
     * @return
     */
    public Params addLlike(String key, String value){
        return this.addlike(key,value,true,false);
    }

    /**
     * 右模糊查询
     * @param key
     * @param value
     * @return
     */
    public Params addRlike(String key, String value){
        return this.addlike(key,value,false,true);
    }

    private Params addlike(String key, String value, boolean left, boolean right){
        if (this.canAdd(value)){
            if (left){
                value = LIKE_PREFIX + value;
            }
            if (right){
                value = value + LIKE_PREFIX;
            }
            this.data.put(key,value);
        }
        return this;
    }

    private boolean canAdd(Object obj){
        if (null == obj){
            return false;
        }
        if (obj instanceof String && StringUtils.isEmpty((String) obj)){
            return false;
        }
        return true;
    }

    private static class ParamsMap extends Params implements Map<String,Object>{
        @Override
        public int size() {
            return super.data.size();
        }

        @Override
        public boolean isEmpty() {
            return super.data.isEmpty();
        }

        @Override
        public boolean containsKey(Object key) {
            return super.data.containsKey(key);
        }

        @Override
        public boolean containsValue(Object value) {
            return super.data.containsValue(value);
        }

        @Override
        public Object get(Object key) {
            return super.data.get(key);
        }

        @Override
        public Object put(String key, Object value) {
            return super.data.put(key,value);
        }

        @Override
        public Object remove(Object key) {
            return super.data.remove(key);
        }

        @Override
        public void putAll(Map<? extends String, ?> m) {
            super.data.putAll(m);
        }

        @Override
        public void clear() {
            super.data.clear();
        }

        @Override
        public Set<String> keySet() {
            return super.data.keySet();
        }

        @Override
        public Collection<Object> values() {
            return super.data.values();
        }

        @Override
        public Set<Entry<String, Object>> entrySet() {
            return super.data.entrySet();
        }
    }
}
